<?php
namespace app\admin\controller;

use \think\Validate as Validate;
/**
 * 问答题类：添加，修改，删除
 * 
 * 
 * @author Aninet Wed Jul 11 19:09:33 2018
 */
class Essay extends Base{
    public function index(){
        $get = $this->req->get();
        $validate = new Validate(['id'=>'require']);
        if($validate->check($get)){
            $model = new \app\admin\model\PaperEssay;
            $data = $model->where(['pap_id'=>$get['id']])->select();
            $this->assign('data',$data);
            return $this->fetch();
        } else {
            return;
        }
    }

    public function essay(){
        $id = $this->req->get('id');
        $this->assign('pid',$id);
        return $this->fetch();
    }

    /**
     * 添加问答题的页面
     * 
     * @return html
     * @date Wed Jul 11 18:48:09 2018
     */
    public function add(){
        $get = $this->req->get();
        $validate = new Validate(['id'=>'require']);
        $model = new \app\admin\model\Paper;
        if($validate->check($get)){
            $paper = $model->where(['pap_id'=>$get['id']])->select();
        } else {
            $paper = $model->where(['pap_type'=>2])->select();
        }
        $this->assign('paper',$paper);
        return $this->fetch();
    }
    
    /**
     * 插入新增试题，接受方式：post
     * 
     * @return 
     * @date Sat Jun  9 20:01:09 2018
     */
    public function insert()
    {
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'pid' => 'require',
            'content' => 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $data = [
                'pap_id' => $post['pid'],
                'es_content' => $post['content'],
                'es_consult' => $post['consult'],
                'es_createtime' => time()
            ];
            $model = new \app\admin\model\PaperEssay;
            $result = $model->save($data);
            if($result){
                $info = [
                    'msg' => '添加成功',
                    'data' => $result
                ];
                echo $this->right($info);
            } else {
                $info = [
                    'msg' => '添加失败',
                    'data' => $result
                ];
                echo $this->error($info);
            }
        } else {
                $info = [
                    'msg' => '你输入的信息不完整',
                ];
                echo $this->wrong($info);
        }            
    }
    public function edit(){
        $get = $this->req->get();
        $validate = new Validate(['id'=>'require']);
        if($validate->check($get)){
            $model = new \app\admin\model\PaperEssay;
            $essay = $model->get($get['id']);
            $model = new \app\admin\model\Paper;
            $paper = $model->field('pap_id,pap_name')->select();
            $this->assign('paper',$paper);
            $this->assign('essay',$essay);
            return $this->fetch();
        }else{
            return;
        }
    }


    public function update(){
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'id' => 'require',
            'pid' => 'require',
            'content' => 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $data = [
                'pap_id' => $post['pid'],
                'es_content' => $post['content'],
                'es_consult' => $post['consult'],
                'es_createtime' => time()
            ];
            $model = new \app\admin\model\PaperEssay;
            $result = $model->where(['es_id'=>$post['id']])->update($data);
            if($result){
                $info = [
                    'msg' => '修改成功',
                    'data' => $result
                ];
                echo $this->right($info);
            } else {
                $info = [
                    'msg' => '修改失败',
                    'data' => $result
                ];
                echo $this->error($info);
            }
        } else {
            $info = [
                'msg' => '你输入的信息不完整',
            ];
            echo $this->wrong($info);
        } 

    }

    public function remove(){
        if($this->req->has('id')){
            $id = $this->req->post('id');
            $model = new \app\admin\model\PaperEssay;
            // $result = $model->where(['es_id'=>$id])->destory();
            $result = 0;
            if($result){
                $info = [
                    'msg' => '删除失败',
                ];
                echo $this->right($info);
            } else {
                $info = [
                    'msg' => '删除成功',
                ];
                echo $this->wrong($info);
            }                
        }else{
            return;
        }
    }

}
